package com.zoke.support.base;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.zoke.support.R;
import com.zoke.support.config.ZokeConfig;
import com.zoke.support.http.CallBackStores;
import com.zoke.support.http.ZokeCallback;
import com.zoke.support.http.ZokeParams;
import com.zoke.support.swipebacklayout.base.SwipeBackActivity;
import com.zoke.support.utils.LogUtil;
import com.zoke.support.utils.MessageHandlerList;
import com.zoke.support.view.TitleManager;
import com.zoke.support.view.ZokeToast;

/**
 * 基类 可以侧滑删除的UI 默认是侧滑删除 可通过获取
 * getSwipeLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_RIGHT)
 * 如果不需要支持侧滑删除 则设置 setSwipeBackEnable(true); 默认支持
 * 
 * @author JackWu
 * 
 */
public class BaseActivity extends SwipeBackActivity implements ZokeCallback {
	protected Handler mHandler;
	protected TitleManager mTitleManager;
	protected Intent mIntent;
	/** 请求类对应的hashCode **/
	protected int mHashCode;

	/** 是否执行双击返回键退出app 默认不支持 **/
	protected boolean mEnableDoubleExit = false;
	private boolean mDoubleExit = false;

	/** 是否支持侧滑删除 默认不支持 **/
	protected boolean mEnableSwipeRight = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 设置MessageHandlerList
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				handleMsg(msg);
			}
		};
		String className = this.getClass().getName();
		MessageHandlerList.addHandler(className, mHandler);
		mHashCode = this.hashCode();
		/** 添加回调 **/
		CallBackStores.getCallback().addCallback(mHashCode, this);
		super.onCreate(savedInstanceState);
		mTitleManager = new TitleManager(BaseActivity.this);
		mIntent = new Intent();
	}

	/** 跳转界面 默认动画 **/
	public void startUI(Intent intent) {
		startActivity(intent);
		overridePendingTransition(R.anim.slide_in_from_left, R.anim.donot_move);
	}

	/** 跳转界面ForResult **/
	public void startUIForResult(Intent intent, int requestCode) {
		startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.slide_in_from_left, R.anim.donot_move);
	}

	/** 动画方式切换UI **/
	protected void startUI(Intent intent, int enterAnim, int exitAnim) {
		overridePendingTransition(enterAnim, exitAnim);
		startUI(intent);
	}

	/** title左按钮点击事件 **/
	public void onTitleClickLeft(View view) {
		onBackPressed();
	}

	/** title标题点击事件 **/
	public void onTitleClickRight(View view) {
	}

	/** title标题右侧点击事件 **/
	public void onTitleClick(View view) {
	}

	@Override
	protected void onResume() {
		super.onResume();
		setSwipeBackEnable(mEnableSwipeRight);
	}

	/**
	 * 接收handle的消息 （ps:一定要调用super.handleMsg())
	 * 
	 * @param msg
	 */
	protected void handleMsg(Message msg) {
		switch (msg.what) {
		case ZokeConfig.ALL_UI_FINISH:
			LogUtil.i("执行界面销毁的操作");
			this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// 移除消息池中对应的handler
		String className = this.getClass().getName();
		MessageHandlerList.removeHandler(className);
		/** 移除callBack **/
		CallBackStores.getCallback().deleteCallback(this.hashCode(), this);
		super.onDestroy();
	}

	@Override
	public void onSuccess(ZokeParams out) {
		// 请求成功的操作代码
	}

	@Override
	public void onFails(ZokeParams out) {
		// 请求失败的操作码
	}

	@Override
	public void onBackPressed() {
		if (!mEnableDoubleExit) {
			super.onBackPressed();
			overridePendingTransition(R.anim.donot_move,
					R.anim.slide_out_from_left);
			return;
		}
		if (mDoubleExit) {
			super.onBackPressed();
			// 执行到双击退出app
			((BaseApp) this.getApplicationContext()).exitApp();
			return;
		}
		mDoubleExit = true;
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				mDoubleExit = false;
			}
		}, 2000);
		// 提示
		ZokeToast.show(getApplicationContext(), this.getString(R.string._exit));
	}
}
